1. Histogram¶
1.1. 概要¶
Histogram(ヒストグラム)とは,例えば下図のように,
1.2. Plotlyによる作図方法¶
Plotlyでは,plotly.express.histogram()でヒストグラムを作成可能です.
import plotly.express as px
fig = px.histogram(df, x='col_x')
上記の例では,dfのcol_x列をX軸,その度数をY軸に取ったヒストグラムのオブジェクトfigを作成します.
1.3. MADB Labを用いた作図例¶
1.3.1. 下準備¶
import pandas as pd
import plotly.express as px
import warnings
warnings.filterwarnings('ignore')
# 前処理の結果,以下に分析対象ファイルが格納されていることを想定
PATH_DATA = '../../data/preprocess/out/magazines.csv'
# Jupyter Book用のPlotlyのrenderer
RENDERER = 'plotly_mimetype+notebook'
def show_fig(fig):
"""Jupyter Bookでも表示可能なようRendererを指定"""
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
fig.show(renderer=RENDERER)
df = pd.read_csv(PATH_DATA)
1.3.2. 作品別の合計連載週数¶
df_plot = df.value_counts('cname').reset_index(name='weeks')
fig = px.histogram(
df_plot, x='weeks', nbins=100,
title='作品別の合計連載週数')
show_fig(fig)
nbinsオプション
plotly.express.histogram()ではnbinsオプジョンでbin数を指定可能です.上記の例では,自動設定で作図するとbinが非常に細かくなってしまうため,便宜的にnbins=100を設定しています.
これでは少し見づらいので,表示範囲をfig.update_xaxis()で変更します.
fig = px.histogram(
df_plot, x='weeks', nbins=100,
title='作品別の合計連載週数')
fig.update_xaxes(range=[0, 200])
show_fig(fig)
1.3.3. 作者別の合計連載週数¶
df_plot = df.value_counts('creator').reset_index(name='weeks')
fig = px.histogram(
df_plot, x='weeks', nbins=100,
title='作者別の合計連載週数')
show_fig(fig)
fig = px.histogram(
df_plot, x='weeks', nbins=100,
title='作者別の合計連載週数')
fig.update_xaxes(range=[0, 200])
show_fig(fig)